﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Complaint.Models.Dtos.Validators;

namespace Complaint.Models.Dtos
{
    public partial class User: IEntity
    {
        public const int MinRequiredPasswordLength = 8;

        public string FullName
        {
            get
            {
                return string.IsNullOrEmpty(Title) 
                    ? string.Format("{0} {1}", FirstName, LastName)
                    : string.Format("{0} {1} {2}", Title, FirstName, LastName);
            }
        }

        public User()
        {
            RegDate = DateTime.Now;
        }

        #region Implementation of IEntity

        public bool IsValid
        {
            get
            {
                return (GetRuleViolations().Count() == 0);
            }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(DisplayName))
            {
                yield return new RuleViolation("You must specify a user name.", "DisplayName");
            }
            if (String.IsNullOrEmpty(Email))
            {
                yield return new RuleViolation("You must specify an email address.", "Email");
            }
            if (!EmailValidator.IsValidEmail(Email))
                yield return new RuleViolation("Please enter a valid email address", "Email");

            if (Password == null || Password.Length < MinRequiredPasswordLength)
            {
                yield return new RuleViolation(String.Format(CultureInfo.CurrentCulture,
                         "You must specify a password of {0} or more characters.",
                         MinRequiredPasswordLength), "Password");
            }
            if (PasswordQuestion == null)
            {
                yield return new RuleViolation("You must select a password question.", "passwordQuestionId");
            }
            if (String.IsNullOrEmpty(PasswordAnswer))
            {
                yield return new RuleViolation("You must specify an answer to the password question.", "PasswordAnswer");
            }
            if (String.IsNullOrEmpty(FirstName))
            {
                yield return new RuleViolation("You must specify a first name.", "FirstName");
            }
            if (String.IsNullOrEmpty(LastName))
            {
                yield return new RuleViolation("You must specify a last name.", "LastName");
            }
        }

        #endregion
    }
}
